package datastructure.book.dp._2_linear_pre_section.single.all;

public class _11_Solution {
    public int deleteAndEarn(int[] nums) {
        int maxValue = nums[0];
        for (int num : nums) {
            maxValue = Math.max(maxValue,num);
        }
        int[] sum = new int[maxValue+1];
        for (int num : nums) {
            sum[num]+=num;
        }
        int[] dp = new int[sum.length];
        dp[0] = 0;dp[1] = sum[1];
        for (int i = 2; i < sum.length; i++) {
            dp[i] = Math.max(dp[i-1],dp[i-2]+sum[i]);
        }
        return dp[dp.length-1];
    }
}
